import Vue from 'vue'
import { timerConversionDate } from '@/filters'
export function fileType(_this, obj) {
	if (_this.title === '资产管理合同') {
		if ('capitalManageContractId' in _this.fundFileInfo) {
			_this.fundFileInfo.capitalManageContractId = { ..._this.fundFileInfo.capitalManageContractId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.capitalManageContractId = {}
			_this.fundFileInfo.capitalManageContractId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.capitalManageContractId)
	} else if (_this.title === '专户其他文件') {
		if ('filingLetterId' in _this.fundFileInfo) {
			_this.fundFileInfo.filingLetterId = { ..._this.fundFileInfo.filingLetterId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.filingLetterId = {}
			_this.fundFileInfo.filingLetterId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.filingLetterId)
	} else if (_this.title === '委托人列表') {
		if ('otherMaterialsId8' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId8 = { ..._this.fundFileInfo.otherMaterialsId8, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId8 = {}
			_this.fundFileInfo.otherMaterialsId8 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId8)
	} else if (_this.title === '金融产品完成备案的证明材料') {
		if ('otherMaterialsId7' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId7 = { ..._this.fundFileInfo.otherMaterialsId7, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId7 = {}
			_this.fundFileInfo.otherMaterialsId7 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId7)
	} else if (_this.title === '投顾协议') {
		if ('otherMaterialsId6' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId6 = { ..._this.fundFileInfo.otherMaterialsId6, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId6 = {}
			_this.fundFileInfo.otherMaterialsId6 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId6)
	} else if (_this.title === '代销协议') {
		if ('otherMaterialsId5' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId5 = { ..._this.fundFileInfo.otherMaterialsId5, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId5 = {}
			_this.fundFileInfo.otherMaterialsId5 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId5)
	} else if (_this.title === '如期货备忘录') {
		if ('otherMaterialsId4' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId4 = { ..._this.fundFileInfo.otherMaterialsId4, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId4 = {}
			_this.fundFileInfo.otherMaterialsId4 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId4)
	} else if (_this.title === '验资报告（集合计划）') {
		if ('otherMaterialsId3' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId3 = { ..._this.fundFileInfo.otherMaterialsId3, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId3 = {}
			_this.fundFileInfo.otherMaterialsId3 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId3)
	} else if (_this.title === '到账通知书') {
		if ('otherMaterialsId2' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId2 = { ..._this.fundFileInfo.otherMaterialsId2, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId2 = {}
			_this.fundFileInfo.otherMaterialsId2 = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId2)
	} else if (_this.title === '成立通知书') {
		if ('otherMaterialsId' in _this.fundFileInfo) {
			_this.fundFileInfo.otherMaterialsId = { ..._this.fundFileInfo.otherMaterialsId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.otherMaterialsId = {}
			_this.fundFileInfo.otherMaterialsId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.otherMaterialsId)
	} else if (_this.title === '风险揭示书') {
		if ('riskProclaimId' in _this.fundFileInfo) {
			_this.fundFileInfo.riskProclaimId = { ..._this.fundFileInfo.riskProclaimId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.riskProclaimId = {}
			_this.fundFileInfo.riskProclaimId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.riskProclaimId)
	} else if (_this.title === '计划说明书') {
		if ('planExplainId' in _this.fundFileInfo) {
			_this.fundFileInfo.planExplainId = { ..._this.fundFileInfo.planExplainId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.planExplainId = {}
			_this.fundFileInfo.planExplainId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.planExplainId)
	} else if (_this.title === '协会备案函') {
		if ('filingLetterId' in _this.fundFileInfo) {
			_this.fundFileInfo.filingLetterId = { ..._this.fundFileInfo.filingLetterId, ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		} else {
			_this.fundFileInfo.filingLetterId = {}
			_this.fundFileInfo.filingLetterId = { ...fileParams(_this.fileTypeMsg, obj, _this.date1) }
		}
		fileView(_this, _this.fundFileInfo.filingLetterId)
	}
}

// 插入文件对象
function fileParams(type, obj, date) {
	if (type === '盖章版') {
		return {
			sealedEffectiveDate: timerConversionDate(date || new Date()), // 生效日期
			sealedFileId: obj.fid, // 文件ID
			sealedTDate: timerConversionDate(new Date()), // 上传日期
			sealedFileName: obj.fname, // 文件名
			sealedFileMd5: obj.fmd5 // 文件fmd5
		}
	} else if (type === 'word版') {
		return {
			firstEffectiveDate: timerConversionDate(date || new Date()),
			firstFileId: obj.fid,
			firstTDate: timerConversionDate(new Date()),
			firstFileName: obj.fname, // 文件名
			firstFileMd5: obj.fmd5 // 文件fmd5
		}
	} else if (type === 'PDF版') {
		return {
			finalEffectiveDate: timerConversionDate(date || new Date()),
			finalFileId: obj.fid,
			finalTDate: timerConversionDate(new Date()),
			finalFileName: obj.fname, // 文件名
			finalFileMd5: obj.fmd5 // 文件fmd5
		}
	}
}

// 插入表格
function fileView(_this, obj) {
	_this.tableData.forEach(item => {
		item.typeName.forEach(items => {
			if (items.title === _this.title) {
				Vue.set(items, 'file', obj)
			}
		})
	})
}

// 删除操作
export function delFileType(_this, title, type) {
	_this.tableData.forEach(item => {
		item.typeName.forEach(items => {
			if (items.title === title) {
				if (type === '盖章版') {
					Vue.delete(items.file, 'sealedEffectiveDate')
					Vue.delete(items.file, 'sealedFileId')
					Vue.delete(items.file, 'sealedTDate')
					Vue.delete(items.file, 'sealedFileName')
					Vue.delete(items.file, 'sealedFileMd5')
				} else if (type === 'word版') {
					Vue.delete(items.file, 'firstEffectiveDate')
					Vue.delete(items.file, 'firstFileId')
					Vue.delete(items.file, 'firstTDate')
					Vue.delete(items.file, 'firstFileName')
					Vue.delete(items.file, 'firstFileMd5')
				} else if (type === 'PDF版') {
					Vue.delete(items.file, 'finalEffectiveDate')
					Vue.delete(items.file, 'finalFileId')
					Vue.delete(items.file, 'finalTDate')
					Vue.delete(items.file, 'finalFileName')
					Vue.delete(items.file, 'finalFileMd5')
				}
			}
		})
	})
	if (title === '资产管理合同') {
		deleteFileproty(_this.fundFileInfo.capitalManageContractId, type)
	} else if (_this.title === '协会备案函') {
		deleteFileproty(_this.fundFileInfo.filingLetterId, type)
	} else if (_this.title === '委托人列表') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId8, type)
	} else if (_this.title === '金融产品完成备案的证明材料') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId7, type)
	} else if (_this.title === '投顾协议') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId6, type)
	} else if (_this.title === '代销协议') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId5, type)
	} else if (_this.title === '如期货备忘录') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId4, type)
	} else if (_this.title === '验资报告（集合计划）') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId3, type)
	} else if (_this.title === '到账通知书') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId2, type)
	} else if (_this.title === '成立通知书') {
		deleteFileproty(_this.fundFileInfo.otherMaterialsId, type)
	} else if (_this.title === '风险揭示书') {
		deleteFileproty(_this.fundFileInfo.riskProclaimId, type)
	} else if (_this.title === '计划说明书') {
		deleteFileproty(_this.fundFileInfo.planExplainId, type)
	}
}

// 删除对象属性
function deleteFileproty(type, obj) {
	if (type === '盖章版') {
		Vue.delete(obj, 'sealedEffectiveDate')
		Vue.delete(obj, 'sealedFileId')
		Vue.delete(obj, 'sealedTDate')
	} else if (type === 'word版') {
		Vue.delete(obj, 'firstEffectiveDate')
		Vue.delete(obj, 'firstFileId')
		Vue.delete(obj, 'firstTDate')
	} else if (type === 'PDF版') {
		Vue.delete(obj, 'finalEffectiveDate')
		Vue.delete(obj, 'finalFileId')
		Vue.delete(obj, 'finalTDate')
	}
}

// 初始化数据时将数据插入数组
export function dataTree(_this) {
	if (_this.fundFileInfo.capitalManageContractId) {
		forEachData(_this, '资产管理合同', 'capitalManageContractId')
	}
	if (_this.fundFileInfo.planExplainId) {
		forEachData(_this, '计划说明书', 'planExplainId')
	}
	if (_this.fundFileInfo.riskProclaimId) {
		forEachData(_this, '风险揭示书', 'riskProclaimId')
	}
	if (_this.fundFileInfo.otherMaterialsId) {
		forEachData(_this, '成立通知书', 'otherMaterialsId')
	}
	if (_this.fundFileInfo.otherMaterialsId2) {
		forEachData(_this, '到账通知书', 'otherMaterialsId2')
	}
	if (_this.fundFileInfo.otherMaterialsId3) {
		forEachData(_this, '验资报告（集合计划）', 'otherMaterialsId3')
	}
	if (_this.fundFileInfo.otherMaterialsId4) {
		forEachData(_this, '如期货备忘录', 'otherMaterialsId4')
	}
	if (_this.fundFileInfo.otherMaterialsId5) {
		forEachData(_this, '代销协议', 'otherMaterialsId5')
	}
	if (_this.fundFileInfo.otherMaterialsId6) {
		forEachData(_this, '投顾协议', 'otherMaterialsId6')
	}
	if (_this.fundFileInfo.otherMaterialsId7) {
		forEachData(_this, '金融产品完成备案的证明材料', 'otherMaterialsId7')
	}
	if (_this.fundFileInfo.otherMaterialsId8) {
		forEachData(_this, '委托人列表', 'otherMaterialsId8')
	}
	if (_this.fundFileInfo.filingLetterId) {
		forEachData(_this, '协会备案函', 'filingLetterId')
	}
}

function forEachData(_this, title, type) {
	_this.tableData.forEach(item => {
		item.typeName.forEach(items => {
			if (items.title === title) {
				Vue.set(items, 'file', _this.fundFileInfo[type])
			}
		})
	})
}

